package com.fm.common.wediget

import android.content.Context
import android.util.AttributeSet
import android.view.MotionEvent
import android.view.View
import android.widget.FrameLayout
import android.widget.ImageView
import android.widget.TextView
import androidx.annotation.ColorRes
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.core.content.ContextCompat
import com.fm.common.R

/**
 *
 * @Author:         ly2
 * @CreateDate:     2021/1/11
 * @Description:    无数据 或 错误提示
 *
 */
class EmptyLayout : FrameLayout, View.OnClickListener {
    private lateinit var mImageView: ImageView
    private lateinit var mTextView: TextView
    private lateinit var mRootView: ConstraintLayout
    private var clickEnable = false
    private var mContext: Context
    private var listener: OnClickListener? = null

    constructor(context: Context) : super(context) {
        this.mContext = context
        init()
    }

    constructor(context: Context, attrs: AttributeSet) : super(context, attrs) {
        this.mContext = context
        init()
    }

    private fun init() {
        val view = View.inflate(mContext, R.layout.empty_error_layout, null)
        mImageView = view.findViewById(R.id.ee_iv_icon)
        mTextView = view.findViewById(R.id.ee_tv_hint)
        mRootView = view.findViewById(R.id.ee_root_view)
        addView(view)
    }

    fun dismiss() {
        clickEnable = false
        visibility = View.GONE
    }

    override fun onClick(v: View) {
        if (clickEnable) {
            listener?.onClick(v)
        }
    }

    fun setBackGroundColor(@ColorRes color: Int) {
        mRootView.setBackgroundColor(ContextCompat.getColor(mContext, color))
    }

    fun setErrorType(type: EmptyType = EmptyType.DEFAULT) {
        visibility = View.VISIBLE
        clickEnable = false
        when (type) {
            EmptyType.NO_SESSION -> {
                mTextView.text = "暂无投注活动"
            }
            EmptyType.NO_BET_SESSION -> {
                mTextView.text = "当前没有方案"
            }
            EmptyType.NO_CHOOSE_SESSION -> {
                mTextView.text = "去选择投注赛事"
            }
            EmptyType.NO_SEARCH_PLAN -> {
                mTextView.text = "未找到相应数据"
            }
            EmptyType.NO_MESSAGE -> {
                mTextView.text = context.getString(R.string.no_data)
            }
            EmptyType.DEFAULT -> {
                mTextView.text = context.getString(R.string.no_data)
            }
            EmptyType.ERROR -> {
                clickEnable = true
                mTextView.text = "加载失败,点击重试"
            }
        }
    }

    fun setOnLayoutClickListener(listener: OnClickListener) {
        this.listener = listener
    }

    /** 重写onInterceptTouchEvent()  */
    override fun onInterceptTouchEvent(ev: MotionEvent): Boolean {
        // 返回true则拦截子控件所有点击事件，如果childclickable为true，则需返回false
        //这样点击整个屏幕都可以刷新控件的，就不需要重写listener的方法，也不需要改变其他的类
        return true
    }


    enum class EmptyType {
        //默认
        DEFAULT,

        //赛事列表无数据
        NO_SESSION,

        //方案列表无数据
        NO_BET_SESSION,

        //投注下单页面赛事列表无数据
        NO_CHOOSE_SESSION,

        //未搜索到方案
        NO_SEARCH_PLAN,

        //站内消息列表无数据
        NO_MESSAGE,

        //错误
        ERROR,
    }

}